package chenxi.guava;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author bowen
 */
public class GMaps {


    public static void main(String[] args) {
//        uniqueIndex();
        Map<Integer, Integer> source = ImmutableMap.of(1, 2, 2, 4);

//        transformEntries(source);

//        biMap();

//        multimap();
//        multimapTransform();
        multiset();
    }

    private static void uniqueIndex() {
        List<String> stringList = ImmutableList.of("1", "11", "111");
        ImmutableMap<Integer, String> map = Maps.uniqueIndex(stringList, String::length);
        System.out.println(map.get(3));
    }

    /**
     * 根据函数改变值
     *
     * @param source param
     */
    private static void transformValues(Map<Integer, Integer> source) {
        Map<Integer, String> map = Maps.transformValues(source, String::valueOf);
    }

    /**
     * @param source param
     */
    private static void transformEntries(Map<Integer, Integer> source) {
        Map<Integer, Integer> map = Maps.transformEntries(source, (integer, integer2) -> integer + integer2);
        System.out.println(map.get(1));
    }

    /**
     * 键过滤
     *
     * @param source param
     */
    private static void filterKeys(Map<Integer, Integer> source) {
        Map<Integer, Integer> filtered = Maps.filterKeys(source, integer -> integer > 10);
    }

    /**
     * 值过滤
     *
     * @param source param
     */
    private static void filterValues(Map<Integer, Integer> source) {
        Map<Integer, Integer> filtered = Maps.filterValues(source, new Predicate<Integer>() {
            @Override
            public boolean apply(Integer integer) {
                return integer > 10;
            }
        });
    }

    /**
     * key value 都必须唯一。
     */
    private static void biMap() {
        BiMap<String, String> biMap = HashBiMap.create();
        biMap.put("a", "b");
        System.out.println(biMap.keySet().size());
    }

    /**
     * 根据键值过滤
     *
     * @param source param
     */
    private static void filterEntries(Map<Integer, Integer> source) {
        Map<Integer, Integer> filtered = Maps.filterEntries(source, new Predicate<Map.Entry<Integer, Integer>>() {
            @Override
            public boolean apply(Map.Entry<Integer, Integer> integerIntegerEntry) {
                return integerIntegerEntry.getKey().compareTo(10) > 0 && integerIntegerEntry.getValue().compareTo(9) > 10;
            }
        });
    }

    /**
     * 值为List
     */
    private static void multimap() {
        ListMultimap<String, String> multimap = ArrayListMultimap.create();
        multimap.put("Fruits", "Banana");
        multimap.put("Fruits", "Apple");
        multimap.put("Fruits", "Pear");
        multimap.put("Fruits", "Pear");
        multimap.put("Vegetables", "Carrot");

        // 5
        System.out.println(multimap.size());

        // Getting values
        List<String> fruits = multimap.get("Fruits");

        List<String> vegetables = multimap.get("Vegetables");

        // Iterating over entire multimap
        for (String value : multimap.values()) {
            System.out.println(value);
        }

        // Removing a single value
        multimap.remove("Fruits", "Pear");
        // [Banana, Apple, Pear]
        System.out.println(multimap.get("Fruits"));

        // Remove all values for a key
        multimap.removeAll("Fruits");
        // [] (Empty Collection!)
        System.out.println(multimap.get("Fruits"));
    }

    /**
     * 值为List
     */
    private static void multimapTransform() {
        Multimap<String, String> multimap = LinkedListMultimap.create();
        multimap.put("a", "a");
        multimap.put("a", "a");

        System.out.println(multimap.get("a"));
        Multimap<String, Integer> transformValues =
                Multimaps.transformValues(
                        multimap,
                        new Function<String, Integer>() {
                            @Override
                            public Integer apply(String str) {
                                return str.length();
                            }
                        });

        System.out.println(transformValues.get("a"));

    }

    /**
     * 允许重复元素的set 元素重复count++
     */
    private static void multiset() {
        Multiset<String> multiset = TreeMultiset.create(Arrays.asList("a", "b", "a", "c"));
        System.out.println(multiset.size());
        System.out.println(multiset.count("a"));
        for (String s : multiset) {
            System.out.println(s);
        }
    }
}
